package csoei.bbs.Dao;
import java.sql.*;
public class UserDao {
	static String sql="";
	static final String driver="com.mysql.jdbc.Driver";
	static final String url="jdbc:mysql://localhost:3306/csoeibbs";
	static final String db_username="root";
	static final String db_password="666666";
	public static void create(String username,String password){
		Connection conn=null;Statement stmt=null;
		sql="insert into user(username,password) values('"+username+"','"+password+"')";
		try {Class.forName(driver);
			try {conn=DriverManager.getConnection(url, db_username, db_password);
				stmt=conn.createStatement();stmt.execute(sql);stmt.close();conn.close();
			} catch (SQLException e) {e.printStackTrace();}
		} catch (ClassNotFoundException e) {e.printStackTrace();}
	}
	public static void update(){
		sql="updating user";
		System.out.println(sql);
	}
	public static void delete(){
		sql="deleting user";
		System.out.println(sql);
	}
	public static int getUsernameById(String username){
		Connection conn=null;Statement stmt=null;ResultSet rs=null;
		sql="select uid from user where username='"+username+"'";
		try {Class.forName("com.mysql.jdbc.Driver");
			try {conn=DriverManager.getConnection(url, db_username, db_password);
				stmt=conn.createStatement();
				rs=stmt.executeQuery(sql);
				if(rs.next()){return rs.getInt(1);}
				rs.close();stmt.close();conn.close();
			} catch (SQLException e) {e.printStackTrace();}
		} catch (Exception e) {e.printStackTrace();}
		return 10000;
		}
	public static boolean validate(String username,String password){
		Connection conn=null;Statement stmt=null;ResultSet rs=null;
		sql="select * from user where username='"+username+"'&&password='"+password+"'";
		try {Class.forName("com.mysql.jdbc.Driver");
			try {conn=DriverManager.getConnection(url, db_username, db_password);
				stmt=conn.createStatement();rs=stmt.executeQuery(sql);
				if(rs.next()){return true;}
				rs.close();stmt.close();conn.close();
			} catch (SQLException e) {e.printStackTrace();}
		} catch (Exception e) {e.printStackTrace();}
		return false;
	}
	public static ResultSet getUserInfo(String uid){
		Connection conn=null;
		Statement stmt=null;
		ResultSet rs=null;
		sql="select j.username,name_display,gender,email,reg_time,address,birthday,site,sign from userinfo i,user j where i.uid=j.uid && j.uid="+uid;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			try {
				conn=DriverManager.getConnection(url, db_username, db_password);
				stmt=conn.createStatement();
				rs=stmt.executeQuery(sql);
				return rs;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}		
		return rs;		
	}
	public static int getUidByName(String username){
		Connection conn=null;Statement stmt=null;ResultSet rs=null;
		sql="select uid from user where username='"+username+"'";
		try {Class.forName("com.mysql.jdbc.Driver");
			try {conn=DriverManager.getConnection(url, db_username, db_password);
				stmt=conn.createStatement();
				rs=stmt.executeQuery(sql);
				if(rs.next()){return rs.getInt(1);}
				rs.close();stmt.close();conn.close();
			} catch (SQLException e) {e.printStackTrace();}
		} catch (Exception e) {e.printStackTrace();}
		return 10000;
	}
}